/*
 * Created: Author: Aléx Carvalho / Date: Feb 24, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-12 00:38:01 -0300 (Fri, 12 Jun 2015) $
 */
package com.cpm.view.zk.treetable.indicator;

import com.cpm.business.SemaphoreColor;
import com.cpm.ServiceLocator;
import com.cpm.view.IconKit;
import com.cpm.entities.Competence;
import com.cpm.entities.Indicator;
import com.cpm.view.CSSClass;
import com.cpm.view.zk.IconElement;
import com.cpm.view.zk.field.LabelField;
import com.cpm.view.Icon;
import com.cpm.wrapper.SolidObjects;
import java.time.Month;
import java.time.Year;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treerow;

public class IndicatorRow {

    private final Indicator indicator;

    private final Year year = Year.now();

    public IndicatorRow(Indicator indicator) {
        SolidObjects.notNull(indicator);

        this.indicator = indicator;
    }

    public void render(Treerow row, int i) {

        LabelField label = new LabelField(indicator.getName());

        Treecell cellName = new Treecell();
        cellName.setSclass(CSSClass.NO_BREAK_LINE.toString());
        cellName.appendChild(label.getElement());
        cellName.setParent(row);
        cellName.setSpan(2);

        getSemaphore(Month.JANUARY).setParent(row);
        getSemaphore(Month.FEBRUARY).setParent(row);
        getSemaphore(Month.MARCH).setParent(row);
        getSemaphore(Month.APRIL).setParent(row);
        getSemaphore(Month.MAY).setParent(row);
        getSemaphore(Month.JUNE).setParent(row);
        getSemaphore(Month.JULY).setParent(row);
        getSemaphore(Month.AUGUST).setParent(row);
        getSemaphore(Month.SEPTEMBER).setParent(row);
        getSemaphore(Month.OCTOBER).setParent(row);
        getSemaphore(Month.NOVEMBER).setParent(row);
        getSemaphore(Month.DECEMBER).setParent(row);
    }

    private Treecell getSemaphore(Month month) {
        Competence c = indicator.getCompetenceList().getCompetenceByYearAndMonth(year, month);

        CSSClass classStyle = ServiceLocator.lookup(SemaphoreColor.class).getSemaphoreColorToCompetence(c);
        
        Icon icon = new Icon(IconKit.SEMAPHORE, classStyle);
        icon.putAttribute(() -> {
            return "font-size:22px;";
        });
        icon.putAttribute(() -> {
            return "text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;";
        });

        IconElement element = new IconElement(icon);

        Treecell cell = new Treecell();
        cell.appendChild(element.getElement());
        cell.setStyle("text-align: center; border-left: 2px solid rgb(167, 167, 167);");

        return cell;
    }

}
